import { OpenidKey } from '@/config';
import ApiClient from '@/utils/ApiClient';

export default {
    requestPay(order_id) {
        return new Promise((resolve, reject) => {
            const openid = wx.getStorageSync(OpenidKey);
            ApiClient.post('/peisong/order/unify', {
                order_id,
                openid,
                app: 'default'
            }).then(response => {
                //console.log(response.data);
                let config = response.result.config;
                if (!config.timeStamp) {
                    config.timeStamp = config.timestamp;
                }
                wx.requestPayment({
                    ...config,
                    success: (res) => {
                        resolve(res);
                    },
                    fail: (res) => {
                        reject(res);
                    }
                    // complete:res => {
                    //     resolve(res);
                    // }
                });
            }).catch(reason => {
                reject(reason);
            });
        });
    }
};
